<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>添加课程</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
  <link rel="stylesheet" href="../../../resources/layuiadmin/layui/css/layui.css" media="all">
  <link rel="stylesheet" href="../../../resources/layuiadmin/style/admin.css" media="all">
</head>
<body>

<div class="layui-fluid">
  <div class="layui-card">
    <div class="layui-card-header">添加课程信息</div>
    <div class="layui-card-body" style="padding: 15px;">
      <form class="layui-form" action="" lay-filter="add-courseInfo">

        <div class="layui-form-item">
          <label class="layui-form-label">课程名</label>
          <div class="layui-input-block">
            <input type="text" name="courseName" lay-verify="required|courseName" autocomplete="off" class="layui-input">
          </div>
        </div>
        <div class="layui-form-item">
          <label class="layui-form-label">课程容量</label>
          <div class="layui-input-block">
            <input type="text" name="courseSize" lay-verify="required" placeholder="" autocomplete="off" class="layui-input">
          </div>
        </div>
        <div class="layui-form-item layui-form-text">
          <label class="layui-form-label">课程简介</label>
          <div class="layui-input-block">
            <textarea name="courseDesc" placeholder="请输入内容" class="layui-textarea"></textarea>
          </div>
        </div>
        <div class="layui-form-item">
          <label class="layui-form-label">授课教师</label>
          <div class="layui-input-block">
            <div class="layui-col-md6">
              <select id="teacher">
              </select>
            </div>
          </div>
        </div>

        <div class="layui-form-item layui-layout-admin">
          <div class="layui-input-block">
            <div class="layui-footer" style="left: 0;">
              <button class="layui-btn" lay-submit="" lay-filter="add-course">立即提交</button>
              <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
          </div>
        </div>

      </form>
    </div>
  </div>
</div>


<script src="../../../resources/layuiadmin/layui/layui.js"></script>
<script src="../../../resources/js/common.js"></script>
<script>
  layui.config({
    base: '../../../resources/layuiadmin/' //静态资源所在路径
  }).extend({
    index: 'lib/index' //主入口模块
  }).use(['index', 'form'], function(){
    var $ = layui.$
            ,layer = layui.layer
            ,form = layui.form;

    form.render(null, 'add-courseInfo');

    /* 自定义验证规则 */
    form.verify({
      courseName: function(value){
        if(containsNumber(value) === true){
          return '课程名不能含有数字';
        }
      }
    });

    getTeacherList();

    // 获取教师列表
    function getTeacherList(){
      $.get('/admin/listTeacherMap', function(res) {
        if (res.success){
          var tempHtml = '';
          // 若访问成功，则依据后台传递过来的教师信息为表单元素赋值
          res.teacherList.map(function (item, index) {
            tempHtml += '<option data-id="' + item.teacherId + '">'
                    + item.teacherName + '</option>';
          });
          $('#teacher').html(tempHtml);
          form.render('select');
        }
      });
    }

    var course = {};

    /* 监听提交 */
    form.on('submit(add-course)', function(){
      course.courseName = $('input[name="courseName"]').val();
      course.courseSize = $('input[name="courseSize"]').val();
      course.courseDesc = $('textarea[name="courseDesc"]').val();
      course.teacher = {
        teacherId : $('#teacher').find('option').not(function () {
          return !this.selected;
        }).data('id')
      };

      var formData = new FormData();
      formData.append('courseStr',JSON.stringify(course));

      //用ajax时需要注意你的url接口、采用哪一种方式type获取，它的使用的哪种数据类型datatype
      $.ajax({
        url:'/admin/addCourse',
        type:'post',
        data:formData,
        contentType : false,
        processData : false,
        success:function(res){
          if (res.success){
            layer.msg("添加课程信息成功");
          }else {
            layer.msg("添加课程信息失败" + res.errMsg);
          }
        }
      })
      return false;
    });

  });
</script>
</body>
</html>
